/**
 签字意见-语音输入转文字
 */
import React, {Component} from 'react';
import {inject, observer} from 'mobx-react';
import {Flex, Icon,} from 'weaver-mobile';
import * as VariableKeys from '../../constants/variableKey';
import * as SignUtil from '../../util/SignUtil';
import {toJS} from 'mobx';

@inject('wfSignInputStore')
@observer
export class SignSoundInput extends Component {
    render() {
        const {wfSignInputStore} = this.props;
        const {controlVariableMap, variableMap} = wfSignInputStore;
        const signVoiceStatus = SignUtil.getVariableMapVal(VariableKeys.SIGN_VOICE_STATUS,false);
        return (
            <div className='sign-sound-input'>
                <Flex className='sound-input-container' justify="around" align='center' direction='column'>
                    <Flex.Item style={{flex: 1}}>{signVoiceStatus ? `点击结束` : '点击说话'}</Flex.Item>
                    <Flex.Item style={{flex: 4}}>
                        <div className='sound-area' onClick={()=>{ this.clickVoice()}}>
                            <Icon type={'voice'}/>
                        </div>
                    </Flex.Item>
                    <Flex.Item style={{flex: 2}}>语音输入，将所说内容转化为文字显示</Flex.Item>
                </Flex>
                <span className='goBack' onClick={() => controlVariableMap(VariableKeys.SIGN_VOICE_ENTRY, false)}>
                    <Icon type={'return'}/>
                </span>
            </div>
        )
    }

    clickVoice(){
        const {wfSignInputStore} = this.props;
        const {controlVariableMap,Adapter,isForwad,variableMap} = wfSignInputStore;
        const signVoiceStatus = SignUtil.getVariableMapVal(VariableKeys.SIGN_VOICE_STATUS,false);
        if(signVoiceStatus) {
            const soundResourceKey = isForwad ? VariableKeys.FORWARD_SIGN_SOUND_RESOURCE : VariableKeys.SIGN_SOUND_RESOURCE;
            const soundDatas = SignUtil.getVariableMapVal(soundResourceKey,[]);
            Adapter.voiceStop((rsource)=>{
                soundDatas.push(rsource);
                controlVariableMap(soundResourceKey,soundDatas);
            });
        } else {
            Adapter.voice();
        }
        controlVariableMap(VariableKeys.SIGN_VOICE_STATUS,!signVoiceStatus);
    }
}

export default SignSoundInput
